package com.ludomancy.sos64;

public class Accumulator
{
	private long totalTime;
	private long lastUpdate;
	private long currentTime;
	
	public Accumulator(long totalTime)
	{
		this.totalTime = totalTime;
		reset();
	}
	
	public void update()
	{
		if(lastUpdate == 0)
			lastUpdate = System.currentTimeMillis();
		currentTime -= (System.currentTimeMillis() - lastUpdate);
		currentTime = Math.max(0, currentTime);
		lastUpdate = System.currentTimeMillis();
	}
	
	public float getRatio()
	{
		return 1.0f - Math.min(1.0f,(float)currentTime/(float)totalTime);
	}
	
	public boolean isRunning()
	{
		return !isDone();
	}
	
	public boolean isDone()
	{
		return currentTime == 0;
	}
	
	public void reset()
	{
		currentTime = totalTime;
		lastUpdate = 0;
	}
	
	public void reset(long newTime)
	{
		totalTime = newTime;
		reset();
	}

}
